﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Management_Nurse_PatientMedication : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadListWard();
            listWard.SelectedIndex = 0;
            loadPatientByWard(int.Parse(listWard.SelectedValue));
            pnInfo.Visible = false;
        }
    }

    private void loadListWard()
    {
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        string sql = "SELECT ID, Name FROM [tbWard]";
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataReader reader = cmd.ExecuteReader();
        listWard.DataSource = reader;
        listWard.DataTextField = "Name";
        listWard.DataValueField = "ID";
        listWard.DataBind();
        connection.Close();
    }
    private void loadPatientByWard(int IDWard)
    {
        string sql = "SELECT [ID], [LastAppointment], [FirstName]+ ' ' + LastName as 'Name' FROM tbPatient INNER JOIN tbImpatient ON tbPatient.[ID]=tbImpatient.[IDPatient] WHERE Status=0 AND tbImpatient.[IDWard]="+IDWard;
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataReader reader = cmd.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        connection.Close();
        pnInfo.Visible = false;
    }
    protected void listWard_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadPatientByWard(int.Parse(listWard.SelectedValue));
    }

    protected void loadInfoPatient(object sender, EventArgs e)
    {
        string[] args = (sender as LinkButton).CommandArgument.Split('&'); 
        int idPatient = Int32.Parse(args[0]);
        int IDAppointment = Int32.Parse(args[1]);
        PatientMedical1.loadInfoPatient(idPatient,IDAppointment);
        pnInfo.Visible = true;
    }
}